package com.gravata.enigma.repositorio.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.gravata.enigma.model.Bouquet;
import com.gravata.enigma.model.Profile;
import com.gravata.enigma.model.Regra;
import com.gravata.enigma.model.Satelite;
import com.gravata.enigma.model.Transponder;
import com.gravata.enigma.repositorio.RepositorioGenerico;
import com.gravata.enigma.repositorio.RepositorioProfile;

public class RepositorioProfileImpl extends RepositorioGenerico<Profile>
		implements RepositorioProfile {

	public RepositorioProfileImpl(Context context) throws SQLException {
		super(context);
	}

	@Override
	public void excluir(List<Profile> entidades) throws SQLException {
		for(Profile profile:entidades){
			excluir(profile);
		}
	}

	@Override
	public void excluir(Profile profile) throws SQLException {
		List<Bouquet> bouquets=new ArrayList<Bouquet>(profile.getBouquets());
		for(Bouquet bouquet:bouquets){
			try{
				bouquet.getServices().clear();
			}catch(Exception e){}
		}
		List<Regra> regras=new ArrayList<Regra>(profile.getRegras());
		for(Regra regra:regras){
			try{
				regra.getFiltros().clear();
			}catch(Exception e){}
		}

		List<Satelite> satelites=new ArrayList<Satelite>(profile.getSatelites());
		for(Satelite satelite:satelites){
			try{
				satelite.getTransponders().clear();
			}catch(Exception e){}
		}


		try{
			profile.getBouquets().clear();
		}catch(Exception e){}
		try{
			profile.getRegras().clear();
		}catch(Exception e){}
		try{
			profile.getServices().clear();
		}catch(Exception e){}

		try{
			profile.getSatelites().clear();
		}catch(Exception e){}

		super.excluir(profile);
	}

	@Override
	public void setSelecionado(Profile profile) {
		List<Profile> profiles=listar();

		for(Profile prof:profiles){
			prof.setSelecionado(prof.equals(profile));
			try {
				atualizar(prof);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public Profile getSelecionado() {
		List<Profile> profiles=listar();

		for(Profile prof:profiles){
			if(prof.isSelecionado())
				return prof;
		}
		return null;
	}

}